﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using IGGCore;
using System.Globalization;
using IGGWindowApp.Controls;

namespace IGGWindowApp.Panels
{
    public partial class PanelTransactions : UserControl
    {
        // EVENTS
        public delegate void ItemSelectedHandler(string item);
        public event ItemSelectedHandler ItemSelected;

        // VAR
        CultureInfo m_culture = new CultureInfo("en-ca");

        /// <summary>
        /// Main constructor
        /// </summary>
        public PanelTransactions()
        {
            InitializeComponent();
        }

        private void panelTitle_ItemSelected(string item)
        {
            ItemSelected(item);
        }

        private void ContentsResized(object sender, ContentsResizedEventArgs e)
        {
            ((AdvRichTextBox)sender).Height = e.NewRectangle.Height + 5;
        }

        /// <summary>
        /// Create an empty space - as a separator
        /// </summary>
        private void CreateEmptySpace()
        {
            Panel panelEmpty = new Panel();
            panelEmpty.BackColor = Color.White;
            panelEmpty.BorderStyle = System.Windows.Forms.BorderStyle.None;
            panelEmpty.Dock = System.Windows.Forms.DockStyle.Top;
            panelEmpty.Location = new System.Drawing.Point(5, 5);
            panelEmpty.Size = new System.Drawing.Size(390, 5);
            panelEmpty.Visible = true;

            panelContent.Controls.Add(panelEmpty);
            panelContent.Controls.SetChildIndex(panelEmpty, 0);
        }

        private void CreateTransTitle(string title)
        {
            AdvRichTextBox label = new IGGWindowApp.Controls.AdvRichTextBox();

            label.BackColor = Color.White;
            label.BorderStyle = System.Windows.Forms.BorderStyle.None;
            label.Dock = System.Windows.Forms.DockStyle.Top;
            label.Font = new System.Drawing.Font("Segoe UI", 8.75F, System.Drawing.FontStyle.Regular);
            label.Location = new System.Drawing.Point(5, 5);
            label.SelectionAlignment = IGGWindowApp.Controls.TextAlign.Left;
            label.Size = new System.Drawing.Size(390, 17);
            label.Text = title;
            label.Visible = true;

            panelContent.Controls.Add(label);
            panelContent.Controls.SetChildIndex(label, 0);
        }


        private void CreateTransContent(string content)
        {
            AdvRichTextBox label = new IGGWindowApp.Controls.AdvRichTextBox();

            label.BackColor = Color.White;
            label.BorderStyle = System.Windows.Forms.BorderStyle.None;
            label.Dock = System.Windows.Forms.DockStyle.Top;
            label.Font = new System.Drawing.Font("Segoe UI", 8.75F, System.Drawing.FontStyle.Regular);
            label.Location = new System.Drawing.Point(5, 5);
            label.SelectionAlignment = IGGWindowApp.Controls.TextAlign.Left;
            label.Size = new System.Drawing.Size(390, 17);
            label.Text = content;
            label.Visible = true;
            label.ContentsResized += new ContentsResizedEventHandler(ContentsResized);

            panelContent.Controls.Add(label);
            panelContent.Controls.SetChildIndex(label, 0);
        }

        private void CreateDate(string date)
        {
            AdvRichTextBox label = new IGGWindowApp.Controls.AdvRichTextBox();

            label.BackColor = Color.White;
            label.BorderStyle = System.Windows.Forms.BorderStyle.None;
            label.Dock = System.Windows.Forms.DockStyle.Top;
            label.Font = new System.Drawing.Font("Segoe UI", 9.75F, System.Drawing.FontStyle.Bold);
            label.Location = new System.Drawing.Point(5, 5);
            label.SelectionAlignment = IGGWindowApp.Controls.TextAlign.Left;
            label.Size = new System.Drawing.Size(390, 20);
            label.Text = date;
            label.Visible = true;

            panelContent.Controls.Add(label);
            panelContent.Controls.SetChildIndex(label, 0);
        }

        /// <summary>
        /// Set the transactions data and update the ui
        /// </summary>
        public void SetData(List <TransactionDay> days)
        {
            if (days == null)
            {
                return;
            }
            int max = 10;
            int count = 1;
            while (count < max)
            {
                foreach (TransactionDay day in days)
                {
                    CreateDate(day.Date.ToString("MMMM d, yyyy", m_culture).ToUpper());
                    foreach (Transaction trans in day.Transactions)
                    {
                        CreateTransTitle(trans.Team + " / " + trans.Player);
                        CreateTransContent(trans.Action);
                        CreateEmptySpace();
                        count++;
                        if (count == max)
                        {
                            break;
                        }
                    }
                    CreateEmptySpace();
                    if (count == max)
                    {
                        break;
                    }
                }
            }
        }
    }
}
